package controller.upms;

import controller.common.BaseController;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import response.CommonResult;
import services.upms.AccountServices;
import util.cryptogram.MD5Util;
import wt.entity.upms.Account;

import javax.servlet.http.HttpServletRequest;

/**
 * 功能:
 * 作者: 洪世成
 * 日期: 2017/9/9
 * 版权所有: admin
 */
@Controller
@RequestMapping("upms/resetPassword")
public class ResetPasswordController extends BaseController {
    @Autowired
    AccountServices accountServices;


    @RequiresPermissions("upms:resetPassword:default")
    @RequestMapping(value = "index")
    public String index() {
        return "upms/resetPassword/index";
    }


    @RequiresPermissions("upms:resetPassword:default")
    @ResponseBody
    @RequestMapping(value = "setPassword")
    public CommonResult setPassword(String oldPassword, String newPassword, HttpServletRequest request) throws Exception {
        CommonResult commonResult = new CommonResult();
        if (StringUtils.isBlank(oldPassword)||StringUtils.isBlank(newPassword)){
            commonResult.setMessage("出现为空数据");
            return  commonResult.setError();
        }



        oldPassword = MD5Util.md5Hex(oldPassword);
        newPassword = MD5Util.md5Hex(newPassword);


        Account account = accountServices.selectByPrimaryKey(getLoginAccount().getId());
        if (!account.getPassword().equals(oldPassword)) {
            commonResult.setMessage("原密码错误");
            return commonResult.setError();

        }

        account.setPassword(newPassword);
        commonResult.setStatus(accountServices.updateByPrimaryKeySelective(account));
        return commonResult;
    }


}